Date.prototype.format = function(fmt) {
    var o = {
        "M+" : this.getMonth()+1,                 //月份
        "d+" : this.getDate(),                    //日
        "h+" : this.getHours(),                   //小时
        "m+" : this.getMinutes(),                 //分
        "s+" : this.getSeconds(),                 //秒
        "q+" : Math.floor((this.getMonth()+3)/3), //季度
        "S"  : this.getMilliseconds()             //毫秒
    };
    if(/(y+)/.test(fmt)) {
        fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
    }
    for(var k in o) {
        if(new RegExp("("+ k +")").test(fmt)){
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
        }
    }
    return fmt;
}



let canSend = false;
$(function() {
    $('#footer').on('keyup', 'input', function() {
        if ($(this).val().length > 0) {
            $(this).next().css('background', '#114F8E').prop('disabled', true);
            canSend = true;
        } else {
            $(this).next().css('background', '#ddd').prop('disabled', false);
            canSend = false;
        }
    })
    $('#footer .send').click(send)
    $("#footer .my-input").keydown(function(e){
        if(e.keyCode == 13){
            return send();
        }
    });


    loadHistoryChats();

    setInterval('loadHistoryChats()',5000);

})

function loadHistoryChats(){
    var fromUserId = $("#fromUserId").val();
    var toUserId = $("#toUserId").val();
    var serializeArray = $("#async_form").serializeArray();
    $.post("/home/api/loadChats/"+fromUserId+"/"+toUserId,serializeArray, function (result) {
        if(result.code===200) {
            $("#lastTime").val(result.endIng);
            $.each(result.data, function(i, item){
                if(item.otype == "ask"){
                    ask(item);
                }else{
                    reply(item);
                }
            });
        }
    });
}

var arr = ['测试1', '测试2', '测试3', '测试4', '测试5'];
// test()
function test() {
    $(arr).each(function(i) {
        setTimeout(function() {
            reply("https://www.jq22.com/demo/jquerychat201907012205/images/touxiang.png", arr[i])
        }, sj() * 500)
    })
}

function reply(_item) {
    var html = "<div class='reply'>" +
        "<div class='time'>"+_item.createDate+"</div>" +
        "<div class='msg'><img src=" + _item.userFromThum + " />" +
        "<span class='name'>"+_item.userName+"</span>" +
        "<p><i class='msg_input'></i>" + _item.msgs + "</p></div>" +
        "</div>";
    return upView(html);
}
function ask(_item) {
    var html = "<div class='ask'>" +
        "<div class='time'>"+_item.createDate+"</div>" +
        "<div class='msg'><img src=" + _item.userFromThum + " />" +
        "<p><i class='msg_input'></i>" + _item.msgs + "</p></div>" +
        "</div>";
    return upView(html);
}
function upView(html) {
    let message = $('#message');
    message.append(html);
    return $('html,body').animate({
        scrollTop: message.outerHeight() - window.innerHeight
    }, 200);
}
function send(msg){
    if(canSend){
        let input = $("#footer .my-input");
        sendMsgInfo(input)
        var dateTime = new Date().format("yyyy-MM-dd hh:mm:ss");
        $("#lastTime").val(dateTime);
        input.val('');
        // test();
    }
}

function sendMsgInfo(input_value){
    var fromUserId = $("#fromUserId").val();
    var toUserId = $("#toUserId").val();
    var form = $("#send-form");
    var serializeArray = form.serializeArray();
    $.post("/home/api/chats/"+fromUserId+"/"+toUserId,serializeArray, function (result) {
        ask(result.data);
    });
    // $.ajax({
    //     type: "post",
    //     url: "/home/api/chats/"+fromUserId+"/"+toUserId+"",
    //     cache: false,
    //     async: false,
    //     dataType: "json",
    //     data: {"msgs":input_value},
    //     success: function (result) {
    //         if(result.code !== 200){
    //         }
    //     }
    // });
}


function sj() {
    return parseInt(Math.random() * 10)
}